package com.dk.controller;

import com.dk.pojo.TabCategory;
import com.dk.service.CategoryService;
import com.dk.service.SellerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@CrossOrigin("*")
@Controller
@RequestMapping("category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    SellerService sellerService;

    @RequestMapping("getInfo")
    @ResponseBody
    public List<TabCategory> getInfo() {
        return categoryService.getInfo();
    }

    @RequestMapping("toAdd")
    public String toAdd() {
        return "Category/add";
    }


    @RequestMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable("id") Integer id, Model model) {
        TabCategory category = categoryService.getInfoById(id);
        model.addAttribute("category", category);
        return "Category/update";
    }

    @RequestMapping("insertInfo")
    @ResponseBody
    public boolean insertInfo(TabCategory category) {
        int i = categoryService.insertInfo(category);
        return i > 0;
    }

    @RequestMapping("updateCategory")
    @ResponseBody
    public boolean updateCategory(TabCategory category) {
        int i = categoryService.updateCategory(category);
        return i > 0;
    }

    @RequestMapping("deleteCategoryById/{id}")
    @ResponseBody
    public boolean deleteCategoryById(@PathVariable("id") Integer id) {
        int i = categoryService.deleteCategoryById(id);
        return i > 0;
    }

    @RequestMapping("upCategory")
    @ResponseBody
    public boolean upCategory(Integer cid){
        boolean i =  categoryService.updateUpCategory(cid);
        return i;
    }

    @RequestMapping("downCategory")
    @ResponseBody
    public boolean downCategory(Integer cid){
        boolean i =  categoryService.updateDownCategory(cid);
        return i;
    }


}
